Thread: uincode char array to array<unsigned char,1>^

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    20

    uincode char array to array<unsigned char,1>^

    As the topic describes I need to convert unicode char array to array<unsigned char,1>^
    Code:
    if ( saveFileDialog1->ShowDialog() == ::DialogResult::OK )
          {
             if ( (myStream = saveFileDialog1->OpenFile()) != nullptr )
             {
    			array<String^>^ stringArray;
    			array<unsigned char,1>^ charArray; 
    			stringArray=this->input->Split('\n');
    			
    			for (int index=0;index<=stringArray->Length;index++){
    				
    				charArray=(array<unsigned char, 1>^)stringArray[index]->ToCharArray();//I get a cast exception here.
    				myStream->Write(charArray,0,charArray->Length);
    			}
    			
    				filename = saveFileDialog1->FileName;
                myStream->Close();
             }
          }
    I also tried marshalling the stringArray[index] to a std::string and using c_str() but that didn't work either.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Wondering. Wouldn't
    Code:
    myStream->Write(stringArray[index]->toCharArray(), 0 , charArray->Length);
    simply work?

    As for why it throws an exception, look here. I guess wchar_t is different from unsigned char?

    Didn't really know that they were casting exceptions for C-style casting...
    Last edited by C_ntua; 12-14-2009 at 08:49 AM.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    20
    Wondering. Wouldn't
    Code:
    myStream->Write(stringArray[index]->toCharArray(), 0 , charArray->Length);
    simply work?

    As for why it throws an exception, look here. I guess wchar_t is different from unsigned char?

    Didn't really know that they were casting exceptions for C-style casting...
    No Write takes array<unsigned char,1>^ not array<w_char>^ of the ToCharArray method. I think w_char are to byte unicode charachters, unsigned char is ascii 1 byte charachters the same as a byte array.

    I'm just learning about these types so if I have this incorrect please respond and let me know.

    That said I still need the correct conversion.

    If there is a work around that would be good. What I'm trying to do is write the contents from the existing file to the new file specified in the save dialog.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    [sigh]
    Last edited by C_ntua; 12-14-2009 at 10:17 AM.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I edited my post and deleted my main point and in general mixed everything...

    wchar_t should be 2 bytes. I believe they depend in general. Anyhow.

    My main point is: ignore the exception. I don't know why it throws it (or by exception you mean compiler warning?). Maybe you could try a C++ cast. It is always better to do so anyway:
    Code:
    charArray = static_cast< array<unsigned char, 1>^ >(stringArray[index]->ToCharArray());
    What you do should work, there is nothing wrong logically with it.

    EDIT: Actually, this conversions are not that simply and should not be done. A solution would be to copy the values stringArray to the charArray, though not the most efficient way.
    The reason why this might not work is that the types are completely different for the compiler.

    Will look more how to cast one template class to another
    Last edited by C_ntua; 12-14-2009 at 10:22 AM.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    20
    I managed a work around by avoiding the use of System::IO::Stream and the write method. Instead I did this:
    Code:
    if ( saveFileDialog1->ShowDialog() == ::DialogResult::OK )
    				 {
    					 writer = gcnew System::IO::StreamWriter(saveFileDialog1->FileName,false);
    					 {
    						 array<String^>^ stringArray;
    						 //array<unsigned char,1>^ charArray; 
    						 stringArray=this->input->Split('\n');
    
    						 for (int index=0;index<=stringArray->Length-1;index++){
    
    							 //charArray=(array<unsigned char, 1>^)stringArray[index]->ToCharArray();
    							 writer->WriteLine(stringArray[index]);
    						 }
    
    						 filename = saveFileDialog1->FileName;
    						 writer->Close();writer = nullptr;
    					 }
    				 }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The UNIX System Interface
    By rrc55 in forum C Programming
    Replies: 1
    Last Post: 10-20-2009, 05:56 PM
  2. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  3. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  4. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM